//
//  HHAccessoryView.m
//  Pods
//
//  Created by Basic. on 2018/6/13.
//

#import "HHAccessoryView.h"
#import <objc/runtime.h>
#import <BCComConfigKit/BCComConfigKit.h>
#import <BCFoundation/UIImage+BCHelper.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunknown-warning-option"
#pragma clang diagnostic ignored "-Wstrict-prototypes"
#define MAS_SHORTHAND
#import <Masonry/Masonry.h>
#pragma clang diagnostic pop

@interface HHAccessoryView ()
@property (nonatomic, strong, readwrite, nullable) UIButton *cancelButton;
@property (nonatomic, strong, readwrite, nullable) UIButton *doneButton;
@property (nonatomic, strong, readwrite, nullable) UILabel *splitLb;
@property (nonatomic, strong, readwrite, nullable) UILabel *topSplitLb;
/// 点击事件
@property (nonatomic, copy, nullable) void(^clickAction)(HHAccessoryView *view, NSInteger index);
@end

@implementation HHAccessoryView
//MARK: - life cycle
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44.0)];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth);
        self.cancelButton.hidden = false;
        self.doneButton.hidden = false;
    }
    return self;
}

#pragma mark - Properties
-(UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelButton.titleLabel.font = BCComConfig.shared.bcFont(16);
        [_cancelButton setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];
        [_cancelButton setTitleColor:BCComConfig.shared.defaultColor forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(toolBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cancelButton];
        [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(9);
            make.top.mas_equalTo(0);
            make.width.mas_equalTo(44);
            make.height.mas_equalTo(self);
        }];
    }
    return _cancelButton;
}
-(UIButton *)doneButton {
    if (!_doneButton) {
        _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _doneButton.titleLabel.font = BCComConfig.shared.bcFont(16);
        [_doneButton setTitle:NSLocalizedString(@"完成", nil) forState:UIControlStateNormal];
        [_doneButton setTitleColor:BCComConfig.shared.defaultColor forState:UIControlStateNormal];
        [_doneButton addTarget:self action:@selector(toolBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_doneButton];
        [_doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-9);
            make.top.mas_equalTo(0);
            make.width.mas_equalTo(44);
            make.height.mas_equalTo(self);
        }];
    }
    return _doneButton;
}
-(UILabel *)splitLb {
    if (!_splitLb) {
        _splitLb = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height-0.4, self.frame.size.width, 0.4)];
        _splitLb.backgroundColor = BCComConfig.config.splitColor;
    }
    return _splitLb;
}
-(UILabel *)topSplitLb {
    if (!_topSplitLb) {
        _topSplitLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0.4)];
        _topSplitLb.backgroundColor = BCComConfig.config.splitColor;
    }
    return _topSplitLb;
}
- (void)setShowTopSplit:(BOOL)showTopSplit {
    _showTopSplit = showTopSplit;
    if (showTopSplit) {
        [self addSubview:self.topSplitLb];
    }
    else {
        [_topSplitLb removeFromSuperview];
        _topSplitLb = nil;
    }
}
-(void)setShowBottomSplit:(BOOL)showBottomSplit {
    _showBottomSplit = showBottomSplit;
    if (showBottomSplit) {
        [self addSubview:self.splitLb];
    }
    else {
        [_splitLb removeFromSuperview];
        _splitLb = nil;
    }
}


#pragma mark - Event
-(void)setMenuClickAction:(void (^)(HHAccessoryView * _Nonnull, NSInteger))clickAction {
    _clickAction = clickAction;
}
- (void)toolBtnClick:(UIButton *)sender {
    NSInteger menuIndex = ((sender == _cancelButton) ? 0 : 1);
    if (self.clickAction) {
        self.clickAction(self, menuIndex);
    }
    else if ([self.delegate respondsToSelector:@selector(HHAccessoryView:didSelectedMenu:)]) {
        [self.delegate HHAccessoryView:self didSelectedMenu:menuIndex];
    }
}

@end

